﻿using System;
//using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.PocketOutlook;
using Google.GData.Calendar;

namespace Synchro
{
    public class Outlook
    {
        private static Outlook _outlook = null;
        private OutlookSession _appSession = new OutlookSession();

        private Outlook() { }

        public static Outlook getOutlook()
        {
            if (_outlook == null)
            {
                _outlook = new Outlook();
            }
            return _outlook;
        }

        public AppointmentCollection getAppointments()
        {
            AppointmentCollection appCol = _appSession.Appointments.Items;
            return appCol;
        }

        public bool addAppointment(string subject, DateTime dateStart, DateTime dateEnd, bool allDay, string location)
        {
            Appointment appt = new Appointment();
            appt.Subject = subject;
            appt.Start = dateStart;
            appt.End = dateEnd;
            appt.AllDayEvent = allDay;
            appt.Location = location;
            try
            {
                _appSession.Appointments.Items.Add(appt);
                return true;
            }
            catch (System.NotSupportedException e)
            {
                return false;
            }
        }

        public bool appointmentExistsInOutlook(EventEntry appt_searched, AppointmentCollection appt_col)
        {
            DateTime start = appt_searched.Times[0].StartTime;//.AddHours(8);
            DateTime end = appt_searched.Times[0].EndTime;//.AddHours(8);

            
            string title = appt_searched.Title.Text;
            Appointment appt = null;
            for (int i = 0; i < appt_col.Count; i++)
            {
                appt = appt_col[i];
                if (appt_searched.Times[0].AllDay) {
                    if ((start.Date.CompareTo(DateTime.Now) <= 0) || 
                        ((appt.Subject == title) && (appt.AllDayEvent.CompareTo(appt_searched.Times[0].AllDay) == 0) 
                        && (appt.Start.CompareTo(start.Date) == 0) ))
                    {
                        return true;
                    }
                }
                else //Eventos con hora
                {
                    if ((start.CompareTo(DateTime.Now) <= 0) || 
                        ((appt.Subject == title) && (appt.Start.CompareTo(start) == 0)
                        && (appt.End.CompareTo(end) == 0) ))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
